package com.songtech.store.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.songtech.common.dictionary.dao.SysDictionaryGroupMapper;
import com.songtech.common.dictionary.dao.SysDictionaryMapper;
import com.songtech.province.bean.Province;
import com.songtech.province.dao.ProvinceMapper;
import com.songtech.store.bean.Store;
import com.songtech.store.bean.StoreRecord;
import com.songtech.store.bean.StoreVo;
import com.songtech.store.dao.StoreMapper;
import com.songtech.store.dao.StoreRecordMapper;
import com.songtech.store.dao.StoreVoMapper;
import com.songtech.store.service.StoreService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: RENTIE
 * Date: 2018-06-19 14:31
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Resource
    private StoreMapper storeMapper;


    @Resource
    private StoreVoMapper storeVoMapper;

    @Resource
    private SysDictionaryGroupMapper sysDictionaryGroupMapper;

    @Resource
    private SysDictionaryMapper sysDictionaryMapper;

    @Resource
    private StoreRecordMapper storeRecordMapper;



    @Override
    public PageInfo<Store> selectInStoreList(Map<String,String> map) throws Exception {
        Integer currentPage = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("limit"));
        PageHelper.startPage(currentPage, pageSize,true,false);
        List<Store> list= storeMapper.selectInStoreList(map);
        if (list!=null){
            list.forEach(l->{
                l.setVender(sysDictionaryGroupMapper.selectNameById(l.getCompanyCode()));
                l.setProduct(sysDictionaryMapper.selectNameById(l.getProductCode()));
                l.setVersion(sysDictionaryMapper.selectNameById(l.getModelNumber()));
                l.setStoreRegionValue(sysDictionaryMapper.selectNameById(l.getStoreRegion()));
            });
        }
        return new PageInfo<Store>(list);
    }

    @Override
    public PageInfo<Store> selectOutStoreList(Map<String,String> map) throws Exception {
        Integer currentPage = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("limit"));
        PageHelper.startPage(currentPage, pageSize,true,false);
        List<Store> list= storeMapper.selectOutStoreList(map);
        if (list!=null){
            list.forEach(l->{
                l.setVender(sysDictionaryGroupMapper.selectNameById(l.getCompanyCode()));
                l.setProduct(sysDictionaryMapper.selectNameById(l.getProductCode()));
                l.setVersion(sysDictionaryMapper.selectNameById(l.getModelNumber()));
            });
        }
        return new PageInfo<Store>(list);
    }

    @Override
    public Integer insertSelective(Store record) throws Exception {
        Integer count = storeMapper.insertSelective(record);
        if (count>0){
            //添加入库记录
            StoreRecord storeRecord = new StoreRecord();
            storeRecord.setCompanyCode(record.getCompanyCode());
            storeRecord.setProductCode(record.getProductCode());
            storeRecord.setModelNumber(record.getModelNumber());
            storeRecord.setOperator(record.getInPerson());
            storeRecord.setOperationTime(record.getInTime());
            storeRecord.setUnitPrice(record.getInUnitPrice());
            storeRecord.setAmount(1);
            storeRecord.setStoreRegion(record.getStoreRegion());
            storeRecord.setStatus("0");
            storeRecord.setRemark(record.getRemark());

            storeRecordMapper.insertSelective(storeRecord);
        }

        return count;

    }

    @Override
    public Integer updateByImeiSelective(Store record) throws Exception {
        Integer count =storeMapper.updateByImeiSelective(record);

        Store tempBean=storeMapper.selectByImei(record.getImei());

        if (count>0){
            //添加出库记录
            StoreRecord storeRecord = new StoreRecord();
            storeRecord.setCompanyCode(tempBean.getCompanyCode());
            storeRecord.setProductCode(tempBean.getProductCode());
            storeRecord.setModelNumber(tempBean.getModelNumber());
            storeRecord.setOperator(record.getOutPerson());
            storeRecord.setOperationTime(record.getOutTime());
            storeRecord.setUnitPrice(record.getOutUnitPrice());
            storeRecord.setAmount(1);
            storeRecord.setPhone(record.getPhone());
            storeRecord.setStatus("1");
            storeRecord.setRemark(record.getRemark());

            storeRecordMapper.insertSelective(storeRecord);
        }
        return count;
    }

    @Override
    public Integer updateByPrimaryKeySelective(Store record) {
        return storeMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Store selectByPrimaryKey(Long id) {
        return storeMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<Store> selectRecordDetailsList(Map<String, String> map) {
        Integer currentPage = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("limit"));
        PageHelper.startPage(currentPage, pageSize,true,false);
        List<Store> list= storeMapper.selectRecordDetailsList(map);
        return new PageInfo<Store>(list);
    }

    @Override
    public int selectAllDevice(Map<String,String> map) {
        return storeMapper.selectAllDevice(map);
    }

    @Override
    public PageInfo<StoreVo> deviceList(Map<String,String> map) throws Exception {
        if ("000000".equals(map.get("saleRegion"))){
            map.put("saleRegion",null);
        }
        Integer currentPage = Integer.valueOf(map.get("page"));
        Integer pageSize = Integer.valueOf(map.get("limit"));
        PageHelper.startPage(currentPage, pageSize,true,false);
        List<StoreVo> list=storeVoMapper.deviceList(map);
        return new PageInfo<StoreVo>(list);
    }


    @Override
    public int deviceOut(Map<String, String> map) throws Exception {
        return storeMapper.deviceOut(map);
    }

    @Override
    public int deviceTodayOut(Map<String, String> map) throws Exception {
        return storeMapper.deviceTodayOut(map);
    }

    @Override
    public int deviceInventory(Map<String, String> map) throws Exception {
        return storeMapper.deviceInventory(map);
    }

    @Override
    public List<Store> deviceOutByDay(Map<String, Object> map) {
        return storeMapper.deviceOutByDay(map);
    }

    @Override
    public List<Store> deviceAllByDay(Map<String, Object> map) {
        return storeMapper.deviceAllByDay(map);
    }

    @Override
    public Map<String,Object> deviceByProvince(Map<String, String> map) throws Exception {

        List<String> provincelist = new ArrayList<String>();

        List<Long> alllist = new ArrayList<Long>();

        List<Long> onlineList=new ArrayList<Long>();

        List<StoreVo> all=storeVoMapper.allDevice(map);
        List<StoreVo> online=storeVoMapper.onlineDevice(map);

        for (int i=0;i<all.size();i++){
            provincelist.add(all.get(i).getPrvName());
            alllist.add(all.get(i).getCount());
            onlineList.add(online.get(i).getCount());
        }

        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("provinc",provincelist);
        resultMap.put("all",alllist);
        resultMap.put("online",onlineList);
        return resultMap;
    }
}
